package main

import (
	"fmt"
	"net/http"
	"shop/admin/admin_api"
	"shop/api"
	"shop/midware"
	"shop/utils/result"

	"github.com/gin-gonic/gin"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

func setRouters(engine *gin.Engine) {
	router := engine.Group("/shop-api")
	// The Handler function provides a default handler to expose metrics
	// via an HTTP server. "/metrics" is the usual endpoint for that.
	router.GET("/metrics", midware.ApiLimiter(5, 10), gin.WrapH(promhttp.Handler()))

	// debug
	{
		router.GET("/ping", midware.ApiLimiter(5, 10), func(ctx *gin.Context) {
			ctx.JSON(http.StatusOK, result.Ok(fmt.Sprintf("启动时间: %v, serverPort: %s", startTime, serverPort)).SetMsg("pong"))
		})
	}

	// auth
	{
		router.POST("/auth/login/weixin", midware.ApiLimiter(20, 100), api.LoginByWeixin)
		router.POST("/auth/loginTestUser", midware.ApiLimiter(1, 5), api.LoginTestUser)
	}

	// user
	{
		router.DELETE("/user/logout", midware.Auth(), midware.UserIdLimiter(1, 5), api.Logout)
		router.GET("/user/info", midware.Auth(), midware.UserIdLimiter(1, 5), api.GetUserInfo)
		router.PUT("/user/info", midware.Auth(), midware.UserIdLimiter(1, 5), api.UpdateUser)
	}

	// goods
	{
		router.GET("/goods/goodsMenu/list", midware.ApiLimiter(100, 1000), api.GetGoodsMenuList)
	}

	// orderInfo
	{
		router.GET("/order/list", midware.Auth(), midware.UserIdLimiter(2, 5), api.GetUserOrderList)
		router.POST("/order", midware.Auth(), midware.UserIdLimiter(1, 1), api.CreateOrder)
		// 初步确认支付完成
		router.PUT("/order/confirmPay/:orderNo", midware.Auth(), midware.UserIdLimiter(1, 5), api.ConfirmFinishPay)
		router.DELETE("/order/cancel/:orderNo", midware.Auth(), midware.UserIdLimiter(1, 10), api.CancelOrder)
		router.PUT("/order/confirmReceive/:orderNo", midware.Auth(), midware.UserIdLimiter(1, 10), api.ConfirmReceive)
	}

	// pay
	{
		// 订单支付
		router.POST("/pay/orderPay/:orderNo", midware.Auth(), midware.UserIdLimiter(1, 1), api.OrderPay)
		// 支付回调
		router.POST("/pay/weixin/notify", midware.ApiLimiter(30, 100), api.WeixinPayNotify)
	}

	// shop
	{
		router.GET("/shop/info", midware.ApiLimiter(100, 1000), api.GetShopInfo)
	}
}

// admin 后台管理接口
func setAdminRouters(engine *gin.Engine) {
	router := engine.Group("/shop-admin-api")

	// 认证与授权
	{
		router.GET("/auth/phoneVerifyCode", midware.IpLimiter(1, 2), admin_api.SendPhoneVerifyCode)
		router.POST("/auth/login", midware.IpLimiter(1, 2), admin_api.Login)
		router.POST("/auth/loginByPhone", midware.IpLimiter(1, 2), admin_api.LoginByPhone)
	}

	// 个人中心
	{
		router.GET("/me/userInfo", midware.AdminAuth(), midware.SysUserIdLimiter(1, 2), admin_api.GetUserInfo)
		router.PUT("/me/updatePassword", midware.AdminAuth("me:updatePassword"), midware.SysUserIdLimiter(1, 2), admin_api.UpdatePassword)
		router.POST("/me/uploadAvatar", midware.AdminAuth("me:uploadUserAvatar"), midware.SysUserIdLimiter(1, 2), admin_api.UploadUserAvatar) // 更新个人头像
		router.DELETE("/me/logout", midware.AdminAuth(), midware.SysUserIdLimiter(1, 2), admin_api.Logout)
	}

	// 用户管理
	{
		router.GET("/user/list", midware.AdminAuth("user:list"), midware.SysUserIdLimiter(1, 10), admin_api.GetUserList)
		router.PUT("/user/status/:userId/:newStatus", midware.AdminAuth("user:update"), midware.SysUserIdLimiter(1, 10), admin_api.UpdateUserStatus) // 启用，冻结，禁用用户账号
	}

	// 商品管理
	{
		router.GET("/goods/list", midware.AdminAuth("goods:list"), midware.SysUserIdLimiter(1, 10), admin_api.GetGoodsList)
		router.POST("/goods", midware.AdminAuth("goods:add"), midware.SysUserIdLimiter(1, 10), admin_api.AddGoods)
		router.PUT("/goods/:id", midware.AdminAuth("goods:update"), midware.SysUserIdLimiter(1, 10), admin_api.UpdateGoods)
		router.POST("/goods/uploadGoodsImg/:id", midware.AdminAuth("goods:update", "goods:uploadGoodsImg"), midware.SysUserIdLimiter(1, 10), admin_api.UploadGoodsImg)
		router.DELETE("/goods/:id", midware.AdminAuth("goods:delete"), midware.SysUserIdLimiter(1, 10), admin_api.DeleteGoods)

		// 商品种类
		router.GET("/goodsCategory/list", midware.AdminAuth("goodsCategory:list"), midware.SysUserIdLimiter(1, 10), admin_api.GetGoodsCategoryList)
		router.POST("/goodsCategory", midware.AdminAuth("goodsCategory:add"), midware.SysUserIdLimiter(1, 10), admin_api.AddGoodsCategory)
		router.PUT("/goodsCategory/:id", midware.AdminAuth("goodsCategory:update"), midware.SysUserIdLimiter(1, 10), admin_api.UpdateGoodsCategory)
		router.DELETE("/goodsCategory/:id", midware.AdminAuth("goodsCategory:delete"), midware.SysUserIdLimiter(1, 10), admin_api.DeleteGoodsCategory)

	}

	// 订单管理
	{
		router.GET("/order/list", midware.AdminAuth("order:list"), midware.SysUserIdLimiter(2, 10), admin_api.GetOrderList)
		router.PUT("/order/status", midware.AdminAuth("order:orderStatus"), midware.SysUserIdLimiter(1, 10), admin_api.UpdateOrderStatus)
		router.POST("/order/print/:id", midware.AdminAuth("order:print"), midware.SysUserIdLimiter(1, 10), admin_api.PrintOrderInfo)
	}

	// 店铺管理
	{
		// 店铺配置
		router.GET("/shop/config", midware.AdminAuth(), midware.SysUserIdLimiter(1, 10), admin_api.GetShopConfig)
		router.PUT("/shop/config", midware.AdminAuth("shopConfig:update"), midware.SysUserIdLimiter(1, 10), admin_api.UpdateShopConfig)
	}
}
